﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Web;

namespace Altairis.Web.UI {
    public static class ExtensionMethods {

        /// <summary>
        /// Removes diacritic marks from string.
        /// </summary>
        /// <param name="s">String to be stripped off diacritic marks.</param>
        /// <returns>Supplied string where all characters with diacritic marks are replaced with their base characters.</returns>
        public static string RemoveDiacritics(this string s) {
            s = s.Normalize(NormalizationForm.FormD);
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < s.Length; i++) {
                if (CharUnicodeInfo.GetUnicodeCategory(s[i]) != UnicodeCategory.NonSpacingMark) sb.Append(s[i]);
            }
            return sb.ToString();
        }

        public static Uri ApplicationBaseUri(this HttpRequest rq) {
            UriBuilder ub = new UriBuilder(rq.Url);
            ub.Path = rq.ApplicationPath;
            if (!ub.Path.EndsWith("/")) ub.Path += "/";
            return ub.Uri;
        }

    }
}
